<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*临时卡管理*/
class Temp extends AdminBase{
	private $user;	
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		$this->user=new \app\admin\model\User();
		parent::_initialize();  
    }
	/*临时卡列表*/
	function index(){
		echo $this->fetch();
	}	
	//新增临时卡
	function add(){
		if(request()->isPost()){
			$card_no=input("card_no");
			$model=db("Interface")->where(" card_no='$card_no' ")->find();
			if(empty($model)){//新增
				db("Interface")->insert(array("card_no"=>$card_no,"owner_type"=>3,"surplus_num"=>1));
			   return $this->ajax_success("成功");
			}else{
		        $owner_type=array("1"=>"会员卡","2"=>"员工卡","3"=>"临时卡");	
				return $this->ajax_error("此卡已经制成了".$owner_type[$model['owner_type']]);
			}
		}
		echo $this->fetch();
	}
	//备注
	function descs(){
		$id=input("id",0);
		if(request()->isPost()){
			db("interface")->where("id=$id")->setField(array("descs"=>input("descs")));
			$this->ajax_success("成功");	
		}else{
			$this->assign("model",db("interface")->where("id=$id")->find());
			echo $this->fetch();	
		}
	}
	//借卡
	function borrow(){
		$card_no=input("card_no");
		if(request()->isPost()){
			$count=db("BorrowCard")->where(" card_no='$card_no' and is_back=0 ")->count(2);
			if($count>0){
				return $this->ajax_error("请先还卡！");
			}
			
			$model=db("Interface")->where(" card_no='$card_no' ")->find();
			if(empty($model)){//新增
				db("Interface")->insert(array("card_no"=>$card_no,"owner_type"=>3,"surplus_num"=>1));
			}else{
				if($model['owner_type']!=3){
					$owner_type=array("1"=>"会员卡","2"=>"员工卡");	
					return $this->ajax_error("此卡已经制成了".$owner_type[$model['owner_type']]);	
				}
			}
			if(empty(input("name"))){
				$name="临时借卡";
			}else{
				$name=input("name");
			}
			db("Interface")->where("card_no='$card_no'")->setField(array("descs"=>input("descs"),"mobile"=>input("mobile"),"name"=>$name));
			db("BorrowCard")->insert(array("descs"=>input("descs"),"mobile"=>input("mobile"),"name"=>$name,"is_back"=>0,"owner_type"=>3,"card_no"=>input("card_no")));
			return $this->ajax_success("成功");
		}else{
			echo $this->fetch();
		}
	}
	//删除卡
	function delete(){
			$id=input("id");
			$count=db("BorrowCard")->where("card_no=(select card_no from m_interface where id=$id and is_back=0)")->find();
			if($count>0){
			  return	$this->ajax_error("请先还卡");
			}
			db("Interface")->where("id=$id")->delete();
			return $this->ajax_success("成功");
	}
	//还卡
	function back(){
		$id=input("id");
		$model=db("Interface")->find($id);
		if($model['owner_type']!=3){
			return $this->ajax_error("此卡不是临时卡");
		}
		if(empty($model['name'])){
			return $this->ajax_error("此卡还没被借出");
		}
		$card_no=$model['card_no'];
		db("Interface")->where("card_no='$card_no' and owner_type=3")->setField(array("name"=>null,"mobile"=>null,"surplus_num"=>1,"descs"=>null));
		db("BorrowCard")->where("card_no='$card_no' and is_back=0")->setField(array("is_back"=>1,"back_time"=>date("Y-m-d H:i:s")));
		$this->ajax_success("成功");
	}
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041001");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
					 'card_no'=>array("field" =>"card_no","operator" =>"like"),
					 'name'=>array("field" =>"name","operator" =>"like"),
					 'mobile'=>array("field" =>"mobile","operator" =>"like"),
					 );
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and owner_type=3 ";
		$total=db("interface")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("interface")->field("fingerprint",true)->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
